package com.tensquare.recruit.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.tensquare.recruit.domain.Enterprise;
import com.tensquare.recruit.service.IEnterpriseService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 企业Controller
 * 
 * @author hbxfwf
 * @date 2021-08-25
 */
@RestController
@RequestMapping("/enterprise")
@Api(tags = "企业管理")
public class EnterpriseController extends BaseController
{
    @Autowired
    private IEnterpriseService enterpriseService;

    /**
     * 查询企业列表
     */
    @PreAuthorize(hasPermi = "recruit:enterprise:list")
    @GetMapping("/list")
    @ApiOperation("查询企业列表")
    public TableDataInfo list(Enterprise enterprise)
    {
        startPage();
        //List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        List<Enterprise> list = enterpriseService.findAll(enterprise);
        return getDataTable(list);
    }


    /**
     * 导出企业列表
     */
    @PreAuthorize(hasPermi = "recruit:enterprise:export")
    @Log(title = "企业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出企业列表")
    public void export(HttpServletResponse response, Enterprise enterprise) throws IOException
    {
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        ExcelUtil<Enterprise> util = new ExcelUtil<Enterprise>(Enterprise.class);
        util.exportExcel(response, list, "企业数据");
    }

    /**
     * 获取企业详细信息
     */
    @PreAuthorize(hasPermi = "recruit:enterprise:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取企业详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(enterpriseService.selectEnterpriseById(id));
    }

    /**
     * 新增企业
     */
    @PreAuthorize(hasPermi = "recruit:enterprise:add")
    @Log(title = "企业", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增企业")
    public AjaxResult add(@RequestBody Enterprise enterprise)
    {
        enterprise.setId(IdUtil.createSnowflake(0,1).nextIdStr());
        //int i = enterpriseService.insertEnterprise(enterprise);
        int id = enterpriseService.insert(enterprise);
        return toAjax(id);
    }

    /**
     * 修改企业
     */
    @PreAuthorize(hasPermi = "recruit:enterprise:edit")
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改企业")
    public AjaxResult edit(@RequestBody Enterprise enterprise)
    {
        return toAjax(enterpriseService.update(enterprise));
    }

    /**
     * 删除企业
     */
    @PreAuthorize(hasPermi = "recruit:enterprise:remove")
    @Log(title = "企业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除企业")
    public AjaxResult remove(@ApiParam(name = "ids",value = "要删除的企业id",required = true) @PathVariable String[] ids)
    {
        return toAjax(enterpriseService.deleteById(ids));
    }
}
